package o2o.task.notice;

import com.jinyou.utils.common.ValidateUtil;
import jPush.dao.JPushMessage;
import o2o.cache.order.OrderInfoMemory;
import o2o.cache.system.SysSettingMemory;
import o2o.cache.user.UserInfoMemory;
import o2o.dao.PO.order.OrderInfo;
import o2o.dao.PO.sys.SysSetting;
import o2o.dao.PO.user.UserInfo;
import o2o.system.data.SYS_SETTINGS;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import wx.actions.WXGZHAction;
import wx.actions.WxGzhActionMan;

import java.util.ArrayList;
import java.util.List;

@Component
public class NoticeTask {

    //需要推送的列表
    public static List<JPushMessage> messageList = new ArrayList<JPushMessage>();

    //定时执行（需要推送的列表）
    //秒  分  时  日  月  星期几  年
    @Scheduled(cron = "0/10 * * * * *")
    public void jPushThread() {
        if (null == SYS_SETTINGS.IS_PUSH_WITH_GZH) {
//            System.out.println("未设置推送");
            return;
        }

        if (SYS_SETTINGS.IS_PUSH_WITH_GZH > 0) {         // 开启极光推送
            if (messageList.size() > 0) {
                List<JPushMessage> tempList = new ArrayList<>(messageList.size());
                synchronized (messageList) {
                    if (ValidateUtil.isAbsList(messageList)) {
//                        tempList.addAll(messageList);
                        tempList = new ArrayList<>(messageList);
                        messageList.clear();
                    }
                }

                if (ValidateUtil.isAbsList(tempList)) {
                } else {
                    return;
                }

                SysSetting sysSetting = SysSettingMemory.getInstance().getByCode("isPushWithGzh");
                if (null == sysSetting) {
                    return;
                }
                for (int i = 0; i < tempList.size(); i++) {
                    JPushMessage info = tempList.get(i);
                    if (null == info) {
                        continue;
                    }

                    String username = info.getUsername();
                    //推给某人
                    if (ValidateUtil.isNull(username)) {
                        continue;
                    }
                    UserInfo userInfo = UserInfoMemory.getInstance().getUserInfo(username);
                    if (null == userInfo) {
                        continue;
                    }

                    switch (SYS_SETTINGS.IS_PUSH_WITH_GZH) {
                        case 1: { // 公众号
                            if (null == sysSetting || ValidateUtil.isNull(sysSetting.getNote())) {
                                continue;
                            }
                            if (ValidateUtil.isNull(userInfo.getWxGzhOpenId())) {
                                continue;
                            }
                            WxGzhActionMan.sendGzhMsg(userInfo.getSysAppKey(), "", userInfo.getWxGzhOpenId(),
                                    sysSetting.getNote(),
                                    info.getTitle(), "",
                                    info.getTitle(), info.getContent());
                        }
                        break;
                        case 2: {// 小程序
                            String orderNo = info.getOrderNo();
                            if (ValidateUtil.isNull(orderNo)) {
                                continue;
                            }
                            OrderInfo orderInfo = OrderInfoMemory.getInstance().getOrderInfo(orderNo);
                            if (null == orderInfo) {
                                continue;
                            }
                            if (ValidateUtil.isNull(userInfo.getWxXcxOpenId())) {
                                continue;
                            }
                            if (ValidateUtil.isNotNull(orderInfo.getPaySign())
                                    && (orderInfo.getPayType().equals("wx_xcx") || orderInfo.getPayType().equals("wx"))
                                    && null != sysSetting && ValidateUtil.isNotNull(sysSetting.getNote())) {
                                WXGZHAction.pushNewOrderMsg(orderInfo.getSysAppKey(), orderInfo.getPayTypeFlag(), sysSetting.getNote(), "", userInfo.getWxXcxOpenId(), orderNo, orderInfo.getShopName(), orderInfo.getShopPhone(), orderInfo.getPostmanPhone());
                            }
                        }
                        break;

                    }

                }
                tempList = null;
            }
        } else {
//            System.out.println("未开启推送");
        }
    }


}
